using UnityEngine;
using System.Collections;

public class NetworkGamePlayer : MonoBehaviour {
	 
	private RigidbodyFPSController fpsController;
	private RespawnPlayerHandler respawnPlayerHandler;
	
	public Material playerSkin;
	public Material[] opponentSkins;
	
	// Use this for initialization
	void Start () {
	}
	
	// Update is called once per frame
	void Update () {
	}
	
	void Awake() {

		fpsController = gameObject.GetComponent<RigidbodyFPSController>();
		respawnPlayerHandler = FindObjectOfType(typeof(RespawnPlayerHandler)) as RespawnPlayerHandler;
		
		
		if (networkView.isMine) {
			
			SkinnedMeshRenderer skin = gameObject.GetComponentInChildren<SkinnedMeshRenderer>();
			
			if (skin != null) {
				skin.material = playerSkin;
			}
			
			gameObject.layer = LayerMgr.LAYER_PLAYER;
			
			BashListener.inst().add(delegate(BashEvent bev) {
				if ((transform.position - bev.getPosition()).magnitude < bev.getDistance()) {
					fpsController.AddBash(bev.getTime());
				}
			});
		} else {
			// Disable NetworkGamePlayer.
			enabled = false;
			
			// Disable opponents controllers.
			if( gameObject.GetComponent<WeaponSystem>() != null)
				gameObject.GetComponent<WeaponSystem>().enabled 			= false;
			if( gameObject.GetComponent<RigidbodyFPSController>() != null)
				gameObject.GetComponent<RigidbodyFPSController>().enabled 	= false;
			if( gameObject.GetComponent<BuildScreen>() != null)
				gameObject.GetComponent<BuildScreen>().enabled 				= false;
			if( gameObject.GetComponent<WeaponZoom>() != null)
				gameObject.GetComponentInChildren<WeaponZoom>().enabled 	= false;
			if( gameObject.GetComponent<SoundSystem>() != null)
				gameObject.GetComponent<SoundSystem>().enabled 	= false;
			//gameObject.rigidbody.isKinematic = true;
			if(gameObject.rigidbody != null)
				gameObject.rigidbody.useGravity = false;
			
			// Set respawn handler.
			if(gameObject.GetComponent<Damagable>() != null)
				gameObject.GetComponent<Damagable>().setRespawnHandler(respawnPlayerHandler);
			
			// Disable cameras.
			Camera[] cameras = gameObject.GetComponentsInChildren<Camera>();
			foreach (Camera cam in cameras) {
				cam.enabled = false;
			}
			
			// Change layer.
			gameObject.layer = LayerMgr.LAYER_OPPONENT;
			
			
			SkinnedMeshRenderer skin = gameObject.GetComponentInChildren<SkinnedMeshRenderer>();
			
			if (skin != null) {
				skin.material = opponentSkins[GetComponent<NID>().getId() % opponentSkins.Length];
			}
		}
		if(Network.isServer){
			Debug.Log("notified");
			GetComponent<NID>().initalise();
			ScoreListener.inst().notify(new ScoreEvent(GetComponent<NID>().getId(),ScoreAction.ADD,0));		
		}
	}
	
	public void disable(){
		enabled = false;
			
			// Disable opponents controllers.
			if( gameObject.GetComponent<WeaponSystem>() != null)
				gameObject.GetComponent<WeaponSystem>().enabled 			= false;
			if( gameObject.GetComponent<RigidbodyFPSController>() != null)
				gameObject.GetComponent<RigidbodyFPSController>().enabled 	= false;
			if( gameObject.GetComponent<BuildScreen>() != null)
				gameObject.GetComponent<BuildScreen>().enabled 				= false;
			if( gameObject.GetComponent<WeaponZoom>() != null)
				gameObject.GetComponentInChildren<WeaponZoom>().enabled 	= false;
			if( gameObject.GetComponent<SoundSystem>() != null)
				gameObject.GetComponent<SoundSystem>().enabled 	= false;
			if(gameObject.rigidbody != null){
				gameObject.rigidbody.isKinematic = true;
				gameObject.rigidbody.useGravity = false;	
		}
			
	}
	
	public void enable(){
		enabled = false;
			
			// Disable opponents controllers.
			if( gameObject.GetComponent<WeaponSystem>() != null)
				gameObject.GetComponent<WeaponSystem>().enabled 			= true;
			if( gameObject.GetComponent<RigidbodyFPSController>() != null)
				gameObject.GetComponent<RigidbodyFPSController>().enabled 	= true;
			if( gameObject.GetComponent<BuildScreen>() != null)
				gameObject.GetComponent<BuildScreen>().enabled 				= true;
			if( gameObject.GetComponent<WeaponZoom>() != null)
				gameObject.GetComponentInChildren<WeaponZoom>().enabled 	= true;
			if( gameObject.GetComponent<SoundSystem>() != null)
				gameObject.GetComponent<SoundSystem>().enabled 	= true;
			if(gameObject.rigidbody != null){
				gameObject.rigidbody.isKinematic = false;
				gameObject.rigidbody.useGravity = true;	
		}
			
	}
	
	void OnSerializeNetworkView(BitStream stream, NetworkMessageInfo info) {
		if (stream.isWriting){
			float speed = fpsController.speed;
			Vector3 pos = transform.position;
			Quaternion rot = transform.rotation;
			stream.Serialize(ref speed);
			stream.Serialize(ref pos);
			stream.Serialize(ref rot);
		} 
		else{
			Vector3 receivedPosition = Vector3.zero;
			Quaternion receivedRotation = new Quaternion(0,0,0,0);
			float speed = 0f;
			stream.Serialize(ref speed);
			fpsController.speed = speed;
			stream.Serialize(ref receivedPosition);
			transform.position = receivedPosition;
			stream.Serialize(ref receivedRotation);
            GetComponentInChildren<WeaponZoom>().transform.rotation = receivedRotation;
		}
	}
	
}
